package org.sith.taskrunner.ejb.services.impl;

import org.sith.taskrunner.api.toolkit.TaskToolkit;
import org.sith.taskrunner.api.user.Role;
import org.sith.taskrunner.ejb.annotations.AllowedRoles;
import org.sith.taskrunner.ejb.db.dao.ToolkitDAO;
import org.sith.taskrunner.ejb.db.entity.TaskToolkitEntity;
import org.sith.taskrunner.ejb.exceptions.TaskToolkitRegisterException;
import org.sith.taskrunner.ejb.services.TaskToolkitRegisterService;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import java.util.List;
import java.util.Set;

@Stateless
public class TaskToolkitManager implements TaskToolkitRegisterService {
    @EJB(beanName = "ToolkitDAOService", beanInterface = ToolkitDAO.class)
    private ToolkitDAO toolkitDAOService;


    @Override
    public void registerToolkitBuilder(TaskToolkitEntity taskToolkit) throws TaskToolkitRegisterException {

        if (taskToolkit.getId() != null || toolkitDAOService.findByNameAndVersion(taskToolkit.getToolkitName(), taskToolkit.getVersion()) != null) {
            throw new TaskToolkitRegisterException("Toolkit already exist");
        }
        toolkitDAOService.saveOrUpdate(taskToolkit);
    }

    @Override
    public void unregisterToolkitBuilder(TaskToolkitEntity toolkit) {
        throw new IllegalStateException("Not implemented yet");
    }

    @Override
    @AllowedRoles({Role.TASK_OWNER, Role.ADMIN})
    public void activateToolkitBuilder(TaskToolkitEntity toolkit) throws TaskToolkitRegisterException {
        boolean result = toolkitDAOService.updateToolkitActiveState(toolkit.getId(), true);
        if (!result) {
            throw new TaskToolkitRegisterException("Toolkit active state was not updated");
        }

    }

    @Override
    @AllowedRoles({Role.TASK_OWNER, Role.ADMIN})
    public void deactivdateToolkitBuilder(TaskToolkitEntity toolkit) throws TaskToolkitRegisterException {
        boolean result = toolkitDAOService.updateToolkitActiveState(toolkit.getId(), false);
        if (!result) {
            throw new TaskToolkitRegisterException("Toolkit active state was not updated");
        }
    }


    @Override
    @AllowedRoles({Role.ADMIN})
    public void activateAllToolkitBuilders() {
        toolkitDAOService.updateAllToolkitActiveState(true);
    }

    @Override
    @AllowedRoles({Role.ADMIN})
    public void deactivateAllToolkitBuilders() {
        toolkitDAOService.updateAllToolkitActiveState(false);
    }

    @Override
    public List<String> getToolkitBuilderNames(Class<? extends TaskToolkit> clazz) {
        throw new IllegalStateException("Not implemented yet");
    }

    @Override
    public TaskToolkitEntity getActiveToolkit(String name, Double version) {
        return toolkitDAOService.getActiveToolkit(name, version);
    }


    @Override
    public List<String> getAllToolkitBuilderNames() {
        throw new IllegalStateException("Not implemented yet");
    }

    @Override
    public List<String> getActiveToolkitsNames() {
        throw new IllegalStateException("Not implemented yet");
    }

    @Override
    @AllowedRoles({Role.ADMIN})
    public void deactivateToolkitBuilders(Class<? extends TaskToolkit> clazz) throws TaskToolkitRegisterException {
        boolean result = toolkitDAOService.updateToolkitActiveState(clazz, false);
        if (!result) {
            throw new TaskToolkitRegisterException("Toolkit active state was not updated");
        }
    }

    @Override
    public boolean isToolkitRegistered(String builderName) {
        throw new IllegalStateException("Not implemented yet");
    }

    @Override
    public void clearToolkits() {
        throw new IllegalStateException("Not implemented yet");
    }

    @Override
    public List<TaskToolkit> getSystemToolkits() {
        return toolkitDAOService.findSystemToolkits();
    }

    @Override
    public List<TaskToolkit> getActiveToolkits() {
        return toolkitDAOService.findActiveToolkits();
    }

    @Override
    public List<TaskToolkit> getAllToolkits() {
        return toolkitDAOService.findAllToolkits();
    }
}
